import { computed, ref, } from 'vue'
import { defineStore } from 'pinia'
import { useUserStore } from './user'
import { insertCartAPI, findNewCartListAPI, deleteCartAPI } from '@/apis/cart'
// import { getCategoryAPI } from '@/apis/layout'
//!购物车模块
export const useCartStore = defineStore('cart', () => {
    var userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    const cartList = ref([])
    //!action 

    //!更新链表
    const getCart = async () => {
        const res = await findNewCartListAPI()
        cartList.value = res.result

    }
    const addCart = async (goods) => {
        const { skuId, count } = goods
        if (isLogin.value) {
            //!登陆了就走接口的加入购物车逻辑
            await insertCartAPI({ skuId, count })
            getCart()
        } else {
            //！走本地逻辑
            //！添加购物车
            //！已添加过 count+1
            //！没添加就push
            //!通过匹配商品skuId,能不能在cartList找到，找到就是添加过
            const item = cartList.value.find((item) => goods.skuId == item.skuId)
            if (item) {
                item.count = ++item.count
            } else {
                cartList.value.push(goods)
            }
        }
    }
    //！单选功能
    const singleCheck = (skuId, selected) => {
        //!通过skuId找到要修改的哪一项，然后修改
        const item = cartList.value.find((item) => item.skuId == skuId)
        item.selected = selected
    }

    //!全选
    //!当所有选项都选择的时候全选
    const isALL = computed(() => cartList.value.every((item) => item.selected))
    //!当点击的时候全选
    const changeAll = (selected) => {
        cartList.value.forEach(item => {
            item.selected = selected
        });
    }
    //！删除购物车
    const delCart = async (skuId) => {
        if (isLogin.value) {
            await deleteCartAPI([skuId])
            //!更新链表
            getCart()
        } else {
            //!思路：1.找到要删除项的下标值~splice
            //!     2.使用数组的过滤方法-fi1ter
            const idx = cartList.value.findIndex((item) => skuId == item.skuId)
            cartList.value.splice(idx, 1)
            // cartList.value = cartList.value.filter((item) => { return skuId != item.item })
        }
    }
    //!清除购物车
    const clearCart = () => {
        cartList.value = []
    }
    //!商品的个数
    //! reduce：total必需。初始值, 或者计算结束后的返回值。currentValue	必需。当前元素

    const allCount = computed(() => cartList.value.reduce((total, currentValue) => total + currentValue.count, 0))
    const allPrice = computed(() => cartList.value.reduce((total, currentValue) => total + currentValue.count * currentValue.price, 0))

    //!已经选择数量 先过滤出select为true的，然后使用reduce累加
    const selectedCount = computed(() => cartList.value.filter((item) => item.selected).reduce((total, currentValue) => total + currentValue.count, 0))
    //！已经选择商品价钱合计
    const selectedPrice = computed(() => cartList.value.filter((item) => item.selected).reduce((total, currentValue) => total + currentValue.count * currentValue.price, 0))

    return { cartList, addCart, delCart, allCount, allPrice, singleCheck, isALL, selectedCount, selectedPrice, changeAll, clearCart, getCart }
}, {
    persist: true
})
